<?php

namespace app\common\model\user;

use basic\ModelBasic;

class UserGoldBills extends ModelBasic
{
	use \traits\ModelTrait;
	
	/**
	 * 发生类型
	 */
	public static $stype = [
		1 => '签到',
		2 => '购物返',
		3 => '兑换商品',
	];
	
	/**
	 * 添加金币账单
	 */
	public static function addBills($uid , $stype, $amt, $remark = '')
	{
		$userObj = \app\common\model\User::field('golds')->where('uid',$uid)->find();
		$userObj->golds += $amt;
		$res = $userObj->save();
		if ( $res === false ) throw new \Exception('Error 10021 [add bills]');
		
		if ( empty($remark) ) {
			$remark = sprintf("%s %s %d个金币" , static::$stype[$stype] , ($amt > 0 ? '获得了':'消耗了') , abs($amt));
		}
		
		$res = static::create([
			'amt'         => $amt,
			'balance'     => $userObj->golds,
			'stype'       => $stype,
			'remark'      => $remark,
			'uid'         => $uid,
			'create_time' => date('Y-m-d H:i:s')
		]);
		if ( $res === false ) throw new \Exception('Error 10022 [add bills]');
		
		return true;
	}
	
}//Class End
